<template>
  <div class="home-page">
    <!-- Banner 区域 -->
    <div class="banner-section">
      <div class="banner-content">
        <h1 class="banner-title">喵趣AI</h1>
        <p class="banner-subtitle">让您的猫咪变身创意主角</p>
        <div class="banner-features">
          <div class="feature-item">
            <van-icon name="photograph" />
            <span>专业证件照</span>
          </div>
          <div class="feature-item">
            <van-icon name="palette" />
            <span>艺术风格</span>
          </div>
          <div class="feature-item">
            <van-icon name="fire" />
            <span>创意无限</span>
          </div>
        </div>
        <van-button 
          type="primary" 
          size="large" 
          round 
          @click="goToUpload"
          class="start-btn"
        >
          开始创作
        </van-button>
      </div>
    </div>

    <!-- 热门风格 -->
    <div class="popular-styles">
      <div class="section-header">
        <h2>热门风格</h2>
        <span class="more-text" @click="goToStyleList">查看全部</span>
      </div>
      
      <div class="styles-grid" v-if="popularStyles.length > 0">
        <div 
          v-for="style in popularStyles" 
          :key="style.id"
          class="style-card"
          @click="selectStyle(style)"
        >
          <div class="style-image">
            <img 
              :src="style.thumbnail_url || style.preview_image_url || '/placeholder.jpg'" 
              :alt="style.name"
              @error="onImageError"
            />
            <div class="style-overlay">
              <span class="style-name">{{ style.name }}</span>
            </div>
          </div>
          <div class="style-info">
            <p class="style-title">{{ style.name }}</p>
            <span class="style-usage">{{ style.usage_count }}次使用</span>
          </div>
        </div>
      </div>
      
      <van-loading v-else class="loading-center" />
    </div>

    <!-- 功能介绍 -->
    <div class="features-section">
      <h2 class="section-title">功能特色</h2>
      <div class="features-list">
        <div class="feature-card">
          <van-icon name="shield-o" class="feature-icon" />
          <h3>AI智能生成</h3>
          <p>先进的AI技术，为您的猫咪打造专属形象</p>
        </div>
        <div class="feature-card">
          <van-icon name="star-o" class="feature-icon" />
          <h3>多样风格</h3>
          <p>从证件照到艺术画，多种风格任您选择</p>
        </div>
        <div class="feature-card">
          <van-icon name="clock-o" class="feature-icon" />
          <h3>快速生成</h3>
          <p>几分钟内完成生成，即时分享给朋友</p>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { useImageStore } from '../stores/image'
import { useUserStore } from '../stores/user'
import type { StyleTemplate } from '../types'
import { Toast } from 'vant'

const router = useRouter()
const imageStore = useImageStore()
const userStore = useUserStore()

const popularStyles = ref<StyleTemplate[]>([])

// 去上传页面
const goToUpload = () => {
  if (!userStore.isLoggedIn) {
    router.push('/login')
    return
  }
  router.push('/upload')
}

// 去风格列表
const goToStyleList = () => {
  router.push('/style')
}

// 选择风格
const selectStyle = (style: StyleTemplate) => {
  if (!userStore.isLoggedIn) {
    router.push('/login')
    return
  }
  
  imageStore.setSelectedStyle(style)
  router.push('/upload')
}

// 图片加载错误处理
const onImageError = (event: Event) => {
  const img = event.target as HTMLImageElement
  img.src = '/placeholder.jpg'
}

// 加载热门风格
const loadPopularStyles = async () => {
  const result = await imageStore.loadStyleTemplates()
  if (result.success) {
    // 取前6个热门风格
    popularStyles.value = imageStore.styleTemplates.slice(0, 6)
  } else {
    Toast.fail(result.error || '加载风格失败')
  }
}

onMounted(() => {
  loadPopularStyles()
})
</script>

<style scoped>
.home-page {
  min-height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}

.banner-section {
  padding: 40px 20px 60px;
  text-align: center;
  color: white;
}

.banner-title {
  font-size: 36px;
  font-weight: bold;
  margin-bottom: 8px;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
}

.banner-subtitle {
  font-size: 16px;
  opacity: 0.9;
  margin-bottom: 30px;
}

.banner-features {
  display: flex;
  justify-content: center;
  gap: 30px;
  margin-bottom: 40px;
}

.feature-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  font-size: 14px;
}

.feature-item .van-icon {
  font-size: 24px;
  opacity: 0.9;
}

.start-btn {
  background: linear-gradient(45deg, #ff6b6b, #feca57);
  border: none;
  box-shadow: 0 4px 15px rgba(255, 107, 107, 0.4);
  font-weight: bold;
}

.popular-styles {
  background: white;
  border-radius: 20px 20px 0 0;
  padding: 30px 20px;
  margin-top: -20px;
  min-height: calc(100vh - 300px);
}

.section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.section-header h2 {
  font-size: 20px;
  font-weight: bold;
  color: #333;
}

.more-text {
  color: #6366f1;
  font-size: 14px;
}

.styles-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 15px;
  margin-bottom: 40px;
}

.style-card {
  background: white;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
  transition: transform 0.3s ease;
}

.style-card:active {
  transform: scale(0.98);
}

.style-image {
  position: relative;
  height: 120px;
  overflow: hidden;
}

.style-image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.style-overlay {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  background: linear-gradient(transparent, rgba(0, 0, 0, 0.7));
  padding: 20px 12px 12px;
  color: white;
}

.style-name {
  font-size: 14px;
  font-weight: bold;
}

.style-info {
  padding: 12px;
}

.style-title {
  font-size: 14px;
  font-weight: bold;
  color: #333;
  margin-bottom: 4px;
}

.style-usage {
  font-size: 12px;
  color: #666;
}

.features-section {
  padding: 0 20px 40px;
}

.section-title {
  font-size: 20px;
  font-weight: bold;
  color: #333;
  text-align: center;
  margin-bottom: 30px;
}

.features-list {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.feature-card {
  background: white;
  padding: 20px;
  border-radius: 12px;
  text-align: center;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
}

.feature-icon {
  font-size: 32px;
  color: #6366f1;
  margin-bottom: 12px;
}

.feature-card h3 {
  font-size: 16px;
  font-weight: bold;
  color: #333;
  margin-bottom: 8px;
}

.feature-card p {
  font-size: 14px;
  color: #666;
  line-height: 1.5;
}

.loading-center {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 200px;
}
</style>